package com.kerrykidz.scminvoicing.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpSession;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPurOrderEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPurordPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmSupEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmSupPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmPurOrderService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.ConstantUtil;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.OptHisVo;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.lang.StringUtil;

/**
 * 
 * @ClassName: ScmPurOrderController
 * @description: 采购订单
 * @author: chenyu
 * @date: 2014年09月06日下午4:39:23
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping("scmInvocing/scmPurOrder")
public class ScmPurOrderController {
	@Autowired
	private IScmPurOrderService iScmPurOrderService;

	@Autowired
	IWorkflowService workflowService;

	@Autowired
	DataSourceUtil das;

	@Autowired
	private ICommonService commonService;

	/**
	 * 采购订单列表查询
	 * 
	 * @Description ModelAndView
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findInvoPurOrder")
	public ModelAndView findInvoOrderPay() throws RDPException {
		ModelAndView mav = new ModelAndView("invoicing/invo_orderManage");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				mav.addObject("isZongBU", 1);
			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branList", branList);
				mav.addObject("isZongBU", 0);
			}
		}
		return mav;
	}

	/**
	 * 采购订单列表查询
	 * 
	 * @Description ScmPurOrderEntity
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findScmPurOrderEntityList")
	@ResponseBody
	public ScmPurOrderEntity findScmPurOrderEntityList(
			ScmPurOrderEntity scmPurOrder) throws RDPException {
		scmPurOrder.setIspaging(true);
		String statrtTime = scmPurOrder.getStartApplyTime();
		if (null != statrtTime && !"".equals(statrtTime)) {
			scmPurOrder.setStartApplyTime(statrtTime + " 00:00:00");
		}
		String endTime = scmPurOrder.getEndApplyTime();
		if (null != endTime && !"".equals(endTime)) {
			scmPurOrder.setEndApplyTime(endTime + " 23:59:59");
		}
		 String userId = UserUtil.getUserFromSession().getUserId();
		 scmPurOrder.setUserId(userId);
		List<ScmPurOrderEntity> scmPurOrderList = iScmPurOrderService
				.findScmPurOrderEntityList(scmPurOrder);
		try {
			if (null != scmPurOrderList && scmPurOrderList.size() > 0) {
				for (ScmPurOrderEntity p : scmPurOrderList) {
					p.setWorkflowVo(workflowService.getWorkflowVo(
							p.getFkFlowinstId(), p.getStatus(),
							UserUtil.getUserFromSession()));
				}
			}
			scmPurOrder.setResults(scmPurOrderList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return scmPurOrder;
	}

	/**
	 * 跳转到 新增采购订单清单页面
	 * 
	 * @Description ModelAndView
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/invoOrderNewGundong")
	public ModelAndView invoOrderNewGundong(
			@RequestParam("pkPurordId") String pkPurordId,
			@RequestParam("supplyId") String supplyId,
			@RequestParam("type") String type) throws RDPException {
		List<OptHisVo> hisWorkFlowList = null;
		String modelView = "invoicing/invo_order_newgd";
		if (type.equals("chuli-tui")) {
			modelView = "invoicing/invo_tuiorder_newgd";
		}
		
		ModelAndView mav = new ModelAndView(modelView);
		String supplyName = "";
		String applyName = "";
		List<ScmSupPrdEntity> listScmSupPrd = null;
		UserInfo user = UserUtil.getUserFromSession();
		String fkBranId = user.getBranId();
		String abbrCode = user.getAbbrCode();
		ScmPurOrderEntity scmPurOrder = null;
		if(abbrCode.toUpperCase().indexOf("ZB") != -1)
		{
			fkBranId = null;
		}
		List<ScmSupEntity> scmSupList = iScmPurOrderService
				.PurOrderSupplyList(fkBranId);
		if (StringUtils.isBlank(pkPurordId)) {
			pkPurordId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.CG,
					user.getAbbrCode());
		} else {
				scmPurOrder = iScmPurOrderService
						.getScmPurOrderById(pkPurordId);
				if (null != scmPurOrder) {
					if(StringUtils.isBlank(supplyId)){
					 supplyId = scmPurOrder.getFkSupId();
					 applyName = scmPurOrder.getApplyName();
					}
					if (StringUtils.isNotBlank(scmPurOrder.getFkFlowinstId())) {
						/** 获取审批流程历史 */
						hisWorkFlowList = workflowService.findOpinions(
								scmPurOrder.getFkFlowinstId(), user);
					}
				}
			listScmSupPrd = iScmPurOrderService.getPurOrderPrdBySupId(
					pkPurordId, supplyId, fkBranId);
			mav.addObject("listScmSupPrd", listScmSupPrd);
		}

		mav.addObject("type", type);
		mav.addObject("hisWorkFlowList", hisWorkFlowList);
		mav.addObject("scmPurOrder", scmPurOrder);
		mav.addObject("scmSupList", scmSupList);
		mav.addObject("supplyName", supplyName);
		mav.addObject("userName", user.getName());
		mav.addObject("brandName", user.getBranName());
		mav.addObject("pkPurordId", pkPurordId);
		mav.addObject("oldSuppLyId", supplyId);
		mav.addObject("applyName", applyName);
		return mav;
	}

	/**
	 * 跳转到 新增采购订单清单页面
	 * 
	 * @Description ModelAndView
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/invoOrderNew")
	public ModelAndView invoOrderNew(
			@RequestParam("pkPurordId") String pkPurordId,
			@RequestParam("type") String type) throws RDPException {
		String modelView = "invoicing/invo_order_new";
		if (type.equals("detail")) {
			modelView = "invoicing/invo_orderDetail";
		} else if (type.equals("chuli")) {
			modelView = "invoicing/invo_orderDetail_y";
		}else if (type.equals("chuli-tui")) {
			modelView = "invoicing/invo_tuiorder_newgd";
		}
		// WorkflowVo workflo= null;
		List<OptHisVo> hisWorkFlowList = null;
		ModelAndView mav = new ModelAndView(modelView);
		UserInfo user = UserUtil.getUserFromSession();
		ScmPurOrderEntity scmPurOrder = null;
		String oldSuppLyId = "";
		String oldSupAgmId = "";
		String supplyName = "";
		String applyName = "";
		String isInFlow = "0";
		List<ScmSupPrdEntity> listScmSupPrd = null;
		String fkBranId = user.getBranId();
		String abbrCode = user.getAbbrCode();
		if(abbrCode.toUpperCase().indexOf("ZB") != -1)
		{
			fkBranId = null;
		}
		List<ScmSupEntity> scmSupList = iScmPurOrderService
				.PurOrderSupplyList(fkBranId);
		if (StringUtils.isBlank(pkPurordId)) {
			pkPurordId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.CG,
					user.getAbbrCode());
		} else {
			scmPurOrder = iScmPurOrderService.getScmPurOrderById(pkPurordId);
			if (null != scmPurOrder) {
				// workflo= workflowService.getWorkflowVo(pkPurordId,
				// scmPurOrder.getStatus(),
				// UserUtil.getUserFromSession(session).getUserId());
				if (StringUtils.isNotBlank(scmPurOrder.getFkFlowinstId())) {
					/** 获取审批流程历史 */
					isInFlow = "1";
					hisWorkFlowList = workflowService.findOpinions(
							scmPurOrder.getFkFlowinstId(), user);
				}

				oldSuppLyId = scmPurOrder.getFkSupId();
				oldSupAgmId = scmPurOrder.getFksupAgmId();
				applyName = scmPurOrder.getApplyName();
				listScmSupPrd = iScmPurOrderService.getPurOrderPrdBySupId(
						pkPurordId, oldSuppLyId, fkBranId);
				mav.addObject("listScmSupPrd", listScmSupPrd);
				for (ScmSupEntity scmSupply : scmSupList) {
					if (oldSuppLyId.equals(scmSupply.getPkSupId())) {
						supplyName = scmSupply.getSupName();
						break;
					}
				}
			}
		}

		mav.addObject("type", type);
		// mav.addObject("workflo",workflo);
		mav.addObject("isInFlow", isInFlow);
		mav.addObject("hisWorkFlowList", hisWorkFlowList);
		mav.addObject("scmPurOrder", scmPurOrder);
		mav.addObject("scmSupList", scmSupList);
		mav.addObject("userName", user.getName());
		 GblSecBranEntity branEntity =new GblSecBranEntity();
		 branEntity.setPkBranId(scmPurOrder.getFkBranId());
		  List<GblSecBranEntity> brandList =commonService.getBranList(branEntity);
		  if(null != brandList){
			  branEntity =brandList.get(0);
		  }
		mav.addObject("brandName", branEntity.getBranName());
	 /*if (type.equals("detail")){//此时的  详细页面 加载 的 门店名称  是 创建 采购人 的 门店。
		 String brandId =scmPurOrder.getFkBranId();
		 GblSecBranEntity branEntity =new GblSecBranEntity();
		 branEntity.setPkBranId(brandId);
		  List<GblSecBranEntity> brandList =commonService.getBranList(branEntity);
		  if(null != brandList){
			  branEntity =brandList.get(0);
		  }
		mav.addObject("brandName", branEntity.getBranName());
	 }else{
		 mav.addObject("brandName", user.getBranName());
	 }*/
		mav.addObject("pkPurordId", pkPurordId);
		mav.addObject("oldSuppLyId", oldSuppLyId);
		mav.addObject("supplyName", supplyName);
		mav.addObject("oldSupAgmId", oldSupAgmId);
		mav.addObject("applyName", applyName);
		return mav;
	}

	/**
	 * 单选时 add采购订单,采购订单商品明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/insertPurOrderPrd")
	@ResponseBody
	public String insertPurOrderPrd(HttpSession session,
			@RequestParam("fkPurordId") String fkPurordId,
			@RequestParam("fkPrdId") String fkPrdId,
			@RequestParam("type") String type,
			@RequestParam("scmSupIds") String supplyId) throws RDPException {
		// UserInfo user=UserUtil.getUserFromSession(session);
		// ScmPurOrderEntity scmPurOrder=null;
		// ScmPurordPrdEntity scmPurordPrd=null;
		// scmPurOrder= iScmPurOrderService.getScmPurOrderById(fkPurordId);
		// ScmSupEntity scmSupEntity =
		// iScmPurOrderService.PurOrderSupplyBySprdIdandSupId( supplyId,
		// fkPrdId);
		String resultMsg = "1";
		// if(null !=scmSupEntity){
		// if(null == scmPurOrder){
		// String fkBranId=user.getBranId();
		// String userId = user.getUserId();
		// Date dateNow =new Date();
		// scmPurOrder =new ScmPurOrderEntity(fkPurordId,null,"1",supplyId,
		// fkBranId,"0",userId,dateNow, userId,dateNow);
		// iScmPurOrderService.addScmPurOrder(scmPurOrder);
		// }else{
		// if(type.equals("cancel")){
		// iScmPurOrderService.delPurOrderPrdByOrderIdAndPrdId(fkPurordId,
		// fkPrdId);
		// }else{
		// scmPurordPrd=iScmPurOrderService.getPurOrderPrdByOrderIdAndPrdId(fkPurordId,
		// fkPrdId);
		// }
		// }
		// if(null ==scmPurordPrd && !type.equals("cancel")){
		// scmPurordPrd=new ScmPurordPrdEntity(fkPurordId,fkPrdId,0,0.0);
		// iScmPurOrderService.insertPurOrderPrd(scmPurordPrd);
		// }
		//
		// }else{
		// resultMsg ="2"; //此商品暂时没有 供应商提供。请先添加供应商及供应商协议。
		// }
		return resultMsg;
	}

	/**
	 * 全选时 批量 add采购订单,采购订单商品明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/addPurOrderPrdBatch")
	@ResponseBody
	public String addPurOrderPrdBatch(HttpSession session,
			@RequestParam("fkPurordId") String fkPurordId,
			@RequestParam("fkPrdIds") String fkPrdIds,
			@RequestParam("type") String type) throws RDPException {
		// UserInfo user = UserUtil.getUserFromSession(session);
		// ScmPurOrderEntity scmPurOrder = null;
		// ScmPurordPrdEntity scmPurordPrd = null;
		// scmPurOrder = iScmPurOrderService.getScmPurOrderById(fkPurordId);
		// String[] fkPrdIdArray =fkPrdIds.split(",");
		// List<ScmPurordPrdEntity> scmPurordPrdList =new
		// ArrayList<ScmPurordPrdEntity>();
		//
		// if (null == scmPurOrder) {//采购订单 数据 未生成 时，先生成 采购订单，再批量 生成初始全选的
		// 订单商品明细。
		// String fkBranId = user.getBranId();
		// String userId = user.getUserId();
		// Date dateNow = new Date();
		// scmPurOrder = new ScmPurOrderEntity(fkPurordId, null, "1",
		// null,fkBranId, "0", userId, dateNow, userId, dateNow);
		// iScmPurOrderService.addScmPurOrder(scmPurOrder);
		// for(String fkPrdId:fkPrdIdArray){
		// List<ScmSupAgmEntity> scmSupAgmList=
		// iScmPurOrderService.findsupAgmListbyPrdId(fkPrdId);
		// if(null !=scmSupAgmList && scmSupAgmList.size()>0){
		// scmPurordPrd = new ScmPurordPrdEntity(fkPurordId, fkPrdId, 0, 0.0);
		// scmPurordPrdList.add(scmPurordPrd);
		// }
		// }
		// iScmPurOrderService.addPurOrderPrdBatch(scmPurordPrdList);
		// } else {//采购订单 数据 已经生成过 时，for循环 先检测 已经生成的 商品明细，再 批量 生成的 订单商品明细。
		// for(String fkPrdId:fkPrdIdArray){
		// if(type.equals("cancel")){
		// iScmPurOrderService.delPurOrderPrdByOrderIdAndPrdId(fkPurordId,
		// fkPrdId);
		// }else{
		// scmPurordPrd =
		// iScmPurOrderService.getPurOrderPrdByOrderIdAndPrdId(fkPurordId,
		// fkPrdId);//for循环 先检测 已经生成的 商品明细，
		// if(null ==scmPurordPrd){//未插入的商品明细，再 批量 生成订单商品明细。
		// List<ScmSupAgmEntity> scmSupAgmList=
		// iScmPurOrderService.findsupAgmListbyPrdId(fkPrdId);
		// if(null !=scmSupAgmList && scmSupAgmList.size()>0){
		// scmPurordPrd = new ScmPurordPrdEntity(fkPurordId, fkPrdId, 0, 0.0);
		// scmPurordPrdList.add(scmPurordPrd);
		// }
		// }
		// }
		// }
		// if(null != scmPurordPrdList && scmPurordPrdList.size()>0 &&
		// !type.equals("cancel")){
		// iScmPurOrderService.addPurOrderPrdBatch(scmPurordPrdList);
		// }
		// }
		//
		return "1";
	}

	/**
	 * 点击 删除 按钮 ，删除采购订单商品明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/delPurOrderPrd")
	@ResponseBody
	public String delPurOrderPrd(HttpSession session,
			@RequestParam("fkPurordId") String fkPurordId,
			@RequestParam("fkPrdId") String fkPrdId) throws RDPException {
		iScmPurOrderService
				.delPurOrderPrdByOrderIdAndPrdId(fkPurordId, fkPrdId);
		return "1";
	}

	/**
	 * 点击 删除 按钮 ，删除采购订单
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/delPurOrder")
	@ResponseBody
	public String delPurOrder(HttpSession session,
			@RequestParam("fkPurordId") String fkPurordId) throws RDPException {
		iScmPurOrderService.delPurOrderPrdByOrderId(fkPurordId);
		iScmPurOrderService.delPurOrderByOrderId(fkPurordId);
		return "1";
	}

	/**
	 * 点击 保存 按钮 ，save采购订单,采购订单商品明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/savePurOrderPrdBatch")
	@ResponseBody
	public String savePurOrderPrdBatch(
			@RequestParam("fkPurordId") String fkPurordId,
			@RequestParam("fkPrdIds") String fkPrdIds,
			@RequestParam("purCnt") String purCnts,
			@RequestParam("agmPrice") String purPrices,
			@RequestParam("scmSupIds") String supplyId,
			@RequestParam("saveType") String saveType,
			@RequestParam("purStatus") String purStatus,
			@RequestParam("fkSupagmId") String fksupAgmIds) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		String fkBranId = user.getBranId();
		String userId = user.getUserId();
		Date dateNow = new Date();
		String resultMsg = "1";// 成功
		ScmPurOrderEntity scmPurOrder = null;
		ScmPurordPrdEntity scmPurordPrd = null;
		scmPurOrder = iScmPurOrderService.getScmPurOrderById(fkPurordId);
		Double sumMoney = 0.0;
		String[] fkPrdIdArray = fkPrdIds.split(",");
		String[] purCntArray = purCnts.split(",");
		String[] purPriceArray = purPrices.split(",");
		String[] fksupAgmIdArray = fksupAgmIds.split(",");
		if (null != scmPurOrder) {
			iScmPurOrderService.delPurOrderPrdByOrderId(fkPurordId);// 先根据 预先 生成
																	// 的采购订单编号，全部删除采购订单明细。

			scmPurOrder.setFkSupId(supplyId);
			scmPurOrder.setApplyBy(userId);
			scmPurOrder.setApplyTime(dateNow);
			scmPurOrder.setModiBy(userId);
			scmPurOrder.setModiTime(dateNow);
			scmPurOrder.setModiTime(new Date());
			iScmPurOrderService.updateScmPurOrder(scmPurOrder);
		} else {
			scmPurOrder = new ScmPurOrderEntity(fkPurordId, null, "1",
					supplyId, fkBranId, "0", userId, dateNow, userId, dateNow);
			iScmPurOrderService.addScmPurOrder(scmPurOrder);
		}
		List<ScmPurordPrdEntity> scmPurordPrdList = new ArrayList<ScmPurordPrdEntity>();
		for (int k = 0; k < fkPrdIdArray.length; k++) {
			Integer purCnt = 0;
			if (!"-".equals(purCntArray[k]) && !"".equals(purCntArray[k])) {
				purCnt = Integer.parseInt(purCntArray[k]);
			}
			if (purCnt > 0) {
				Double purPrice = purCnt * Double.parseDouble(purPriceArray[k]);
				sumMoney += purPrice;
				scmPurordPrd = new ScmPurordPrdEntity(fkPurordId,
						fksupAgmIdArray[k], fkPrdIdArray[k], purCnt, purPrice);
				scmPurordPrdList.add(scmPurordPrd);
			}
		}
		if (null != scmPurordPrdList && scmPurordPrdList.size() > 0) {
			iScmPurOrderService.addPurOrderPrdBatch(scmPurordPrdList);
		}
		if (saveType.equals("tijiao")) {
			ScmSupEntity scmSup = iScmPurOrderService
					.getScmSupBysupId(supplyId);
			if (null == scmSup) {
				resultMsg = "2";// 供应商不存在，不能提交审批。
			} else {
				if (StringUtils.isBlank(scmPurOrder.getFkFlowinstId())) {// 流程id为空时，设状态1
																			// ，
					purStatus = "1";// 1 草稿
				} else {
					purStatus = "2";// 2 审批中
				}
				if (purStatus.equals("1")) {// 门店提交订单审核
					String fkFlowId = workflowService.startWorkflow(ConstantUtil.getWorkflowKey(ConstantUtil.WorkflowKey.order),fkPurordId, user);
					 if(StringUtil.isNotBlank(fkFlowId)){
						 	scmPurOrder.setFkFlowinstId(fkFlowId);
							scmPurOrder.setStatus("2");
							scmPurOrder.setModiTime(new Date());
							iScmPurOrderService.updateScmPurOrder(scmPurOrder);
							//判断是否是门店
							if(StringUtil.isNotBlank(user.getBranId())){
								//如果是门店的话就进行数据上传
								if(user.getBranId().indexOf("ZB") == -1){
									ScmPurOrderEntity spo = iScmPurOrderService.getScmPurOrderById(scmPurOrder.getPkPurordId());
									List<ScmPurordPrdEntity> scmPurordPrdListOld = iScmPurOrderService.findPurOrderPrdListByOrderId(scmPurOrder.getPkPurordId());
									spo.setPurOrdPrdList(scmPurordPrdListOld);
									das.executeOrder(spo, "insert");
								}
							}
						}
					
				}
				if (purStatus.equals("2")) {//退回 重新提交 审批
					WorkflowParamVo workflowparam = new WorkflowParamVo();
					workflowparam.setHandleAdviceDes(ConstantUtil.WORKFLOW_STATUS_REAPPLY);
					workflowparam.setHandleParam("orderAmount");
					workflowparam.setHandleParamValue(sumMoney);
					workflowparam.setBusinessKey(fkPurordId);
					workflowparam.setProcessInstanceId(scmPurOrder
							.getFkFlowinstId());
					workflowparam.setUserInfo(user);
					//判断是否是门店
					if(StringUtil.isNotBlank(user.getBranId())){
						//如果是门店的话就进行数据上传
						if(user.getBranId().indexOf("ZB") == -1){
							ScmPurOrderEntity spo = iScmPurOrderService.getScmPurOrderById(scmPurOrder.getPkPurordId());
							List<ScmPurordPrdEntity> scmPurordPrdListOld = iScmPurOrderService.findPurOrderPrdListByOrderId(scmPurOrder.getPkPurordId());
							spo.setPurOrdPrdList(scmPurordPrdListOld);
							das.executeOrder(spo, "update");
						}
					}
					workflowService.complete(workflowparam);
					/*if (processInstance == null) {
						if(StringUtils.isNotBlank(workflowparam.getHandleAdvice()))
							scmPurOrder.setStatus(workflowparam.getHandleAdvice().equals("1") ? "3" : "4");
						scmPurOrder.setModiTime(new Date());
						iScmPurOrderService.updateScmPurOrder(scmPurOrder);
					}*/
				}
			}
		}
		return resultMsg;
	}

	/**
	 * 字符串数组 去重
	 * 
	 * @Description String[]
	 * @author chenyu 2014年9月9日 下午7:57:09
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String[] getStrToArray(String Strs) {
		String[] str = Strs.split(",");
		Set set = new TreeSet();
		for (int i = 0; i < str.length; i++) {
			set.add(str[i]);
		}
		str = (String[]) set.toArray(new String[0]);
		return str;
	}

	/**
	 * update采购订单
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/updateScmPurOrder")
	@ResponseBody
	public String updateScmPurOrder(HttpSession session,
			ScmPurOrderEntity scmPurOrder) throws RDPException {
		// UserInfo user=UserUtil.getUserFromSession(session);
		// String pkPayformId=PrimaryKeyGenerator.getPrimaryKey(BusinessType.CG,
		// user.getAbbrCode());
		// String abbrCode=user.getBranId();
		// String userId = user.getUserId();
		// iScmPurOrderService.updateScmPurOrder(scmPurOrder);
		return "1";
	}

	/**
	 * 根据采购订单编号。查询采购订单商品 明细 数据
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findPurOrderPrdByOrderId")
	@ResponseBody
	public Map<String, Object> findPurOrderPrdByOrderId(
			@RequestParam("fkPurordId") String fkPurordId,
			@RequestParam("supplyId") String supplyId) throws RDPException {
		List<ScmPurordPrdEntity> listScmPurordPrd = iScmPurOrderService
				.findPurOrderPrdByOrderId(fkPurordId, supplyId);
		Map<String, Object> mapobj = new HashMap<String, Object>();
		// if(null != listScmPurordPrd && listScmPurordPrd.size()>0){
		// for(ScmPurordPrdEntity scmPurOrderPrd:listScmPurordPrd){
		// List<ScmSupEntity> scmSupList =
		// iScmPurOrderService.PurOrderSupplyListBySprdId(scmPurOrderPrd.getFkPrdId());
		// if(null != scmSupList && scmSupList.size()>0){
		// scmPurOrderPrd.setScmSupList(scmSupList);
		// }
		// }
		// }
		mapobj.put("listScmPurordPrd", listScmPurordPrd);
		return mapobj;
	}

	/**
	 * 根据采购订单编号，门店编号。查询可用的商品 数据
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/getPurOrderPrdBySupId")
	@ResponseBody
	public Map<String, Object> getPurOrderPrdBySupId(
			@RequestParam("supplyId") String supplyId) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		List<ScmSupPrdEntity> listScmSupPrd = iScmPurOrderService
				.getPurOrderPrdBySupId("", supplyId, user.getBranId());
		Map<String, Object> mapobj = new HashMap<String, Object>();
		mapobj.put("listScmSupPrd", listScmSupPrd);
		return mapobj;
	}

	/***
	 * 审批流程 提交
	 * 
	 * @param param
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPurOrder")
	public ModelAndView approvalProcessPromotionSub(WorkflowParamVo param)
			throws RDPException {
		ModelAndView model = new ModelAndView("invoicing/invo_orderManage");
		ScmPurOrderEntity entity = new ScmPurOrderEntity();
		entity = iScmPurOrderService.getScmPurOrderById(param.getBusinessKey());
		Double sumMoney = 0.0;
		param.setUserInfo(UserUtil.getUserFromSession());
		List<ScmPurordPrdEntity> scmPurOrderPrdList = iScmPurOrderService
				.findPurOrderPrdListByOrderId(param.getBusinessKey());
		if (null != scmPurOrderPrdList && scmPurOrderPrdList.size() > 0) {
			for (ScmPurordPrdEntity scmOrdPrd : scmPurOrderPrdList) {
				sumMoney += scmOrdPrd.getPurPrice();
			}
		}
		param.setHandleParam("orderAmount");
		param.setHandleParamValue(sumMoney);
		ProcessInstance processInstance = workflowService.complete(param);
		if (processInstance == null) {
			entity.setStatus(param.getHandleAdvice().equals("1") ? "3" : "4");
			entity.setModiTime(new Date());
			iScmPurOrderService.updateScmPurOrder(entity);
		}
		return model;
	}

	/***
	 * 审批流程 退回
	 * 
	 * @param param
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPurOrderExt")
	public ModelAndView approvalProcessPromotionExt(WorkflowParamVo param)
			throws RDPException {
		ModelAndView model = new ModelAndView("invoicing/invo_orderManage");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				model.addObject("branList", branList);
				model.addObject("isZongBU", 1);
			} else {
				branList = commonService.getBranListForStock(brandId);
				model.addObject("branList", branList);
				model.addObject("isZongBU", 0);
			}
		}
		ScmPurOrderEntity entity = new ScmPurOrderEntity();

		entity = iScmPurOrderService.getScmPurOrderById(param.getBusinessKey());

		param.setUserInfo(UserUtil.getUserFromSession());
		workflowService.backProcessToStart(param);
		entity.setStatus("2");
		entity.setModiTime(new Date());
		iScmPurOrderService.updateScmPurOrder(entity);
		return model;
	}

	@RequestMapping("/findProductListJoinOrderPrd")
	@ResponseBody
	public GblScmPrdEntity findProductListJoinOrderPrd(GblScmPrdEntity entity)
			throws RDPException {
		if (entity.getExcludeId() == null || entity.getExcludeId().size() == 0) {
			entity.setExcludeId(null);
		}
		entity.setIspaging(true);
		entity.setDelFlag("0");
		List<GblScmPrdEntity> resultList = iScmPurOrderService
				.findProductListJoinOrderPrd(entity);
		entity.setResults(resultList);
		return entity;
	}

//	@RequestMapping("/checkScmSupExist")
//	@ResponseBody
//	public String checkScmSupExist(@RequestParam("supplyId") String supplyId)
//			throws RDPException {
//		String resultMsg = "1";
//		ScmSupEntity scmSup = iScmPurOrderService.getScmSupBysupId(supplyId);
//		if (null == scmSup) {
//			resultMsg = "2";// 供应商不存在，不能提交审批。
//		}
//		return resultMsg;
//	}
}
